/*
 * Created on Jan 22, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.swing;

import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SpringLayout;
import org.apache.log4j.*;

public class SpringGrid extends JPanel {

	private static final long serialVersionUID = 5871420324650311681L;

	private transient static Logger log = Logger.getLogger(SpringGrid.class);
	
	private int columnCount = 2;
	private int rowCount = 0;
	private int xPadding = 2;
	private int yPadding = 2;
	private int x = xPadding;
	private int y = yPadding;
	private JLabel errorPanel = new JLabel();
	private int numberOfEmptyCellsAtEnd = 0;  // Calculated in pack();
	private boolean compact = true;
	
	private Vector componentList = new Vector();

	/**
	 * 
	 */
	public SpringGrid() {
		super();
		this.setLayout(new SpringLayout());
		errorPanel.setText("Instance of SpringGrid did not call pack() after adding cells");
		super.add(errorPanel);
	}

	public void add(JComponent component) {
		addComponent(component);
	}

	public void addComponent(JComponent component) {
		log.debug("addComponent() - adding component to next cell");
		log.info("addComponent() - class = " + component.getClass().getName());
		log.debug("Component is not a JTextArea, just add it");
		componentList.add(component);
	}
	
	public void pack() {
		log.debug("pack() - this method will format grid");
		this.removeAll();
		rowCount = componentList.size() / columnCount;
		if (rowCount < 1) {
			log.debug("No rows were found, creating one that will have blank cells");
			rowCount = 1;
		}
		for (int i = 0; i < componentList.size(); i++) {
			JComponent component = (JComponent)componentList.elementAt(i);
			super.add(component);
		}
		
		
		int numberOfEmptyCellsAtEnd = 0; 
		if (componentList.size() < (columnCount * rowCount)) {
			log.debug("Need to add empty cells to end in order to complete the last row...");
			if (componentList.size() > 0) {
				rowCount++;
			}
			numberOfEmptyCellsAtEnd = (columnCount * rowCount) - componentList.size();
			addEmptyPanelsToEnd(numberOfEmptyCellsAtEnd);
		}
		
		if (log.isDebugEnabled()) {
			log.debug("pack() - componentList.size = " + componentList.size());
			log.debug("pack() - numberOfEmptyCells = " + numberOfEmptyCellsAtEnd);
			log.debug("pack() - columnCount = " + columnCount);
			log.debug("pack() - rowCount = " + rowCount);
		}
		log.debug("pack() - this method will format grid");
		
		if (rowCount > 0) {
			log.debug("Configuring the grid...");
			if (isCompact()) {
				SpringUtilities.makeCompactGrid(this, getRowCount(), getColumnCount(), 
						this.getX(), this.getY(), 
						this.getXPadding(), this.getYPadding());
			}
			else {
				SpringUtilities.makeGrid(this, getRowCount(), getColumnCount(), 
						this.getX(), this.getY(), 
						this.getXPadding(), this.getYPadding());
			}
		}
		this.validate();
		
	}
	
	private void addEmptyPanelsToEnd(int numberOfEmptyCellsAtEnd) {
		for (int i = 0; i < numberOfEmptyCellsAtEnd; i++) {
			log.debug("Adding empty panel cell");
			JPanel panel = new JPanel();
			super.add(panel);
		}
	}
	
	
	/**
	 * @return
	 */
	public int getColumnCount() {
		return columnCount;
	}

	/**
	 * @param columns
	 */
	public void setColumnCount(int columns) {
		this.columnCount = columns;
	}

	/**
	 * @return
	 */
	public int getX() {
		return x;
	}

	/**
	 * @param x
	 */
	public void setX(int x) {
		this.x = x;
	}

	/**
	 * @return
	 */
	public int getXPadding() {
		return xPadding;
	}

	/**
	 * @param padding
	 */
	public void setXPadding(int padding) {
		xPadding = padding;
	}

	/**
	 * @return
	 */
	public int getY() {
		return y;
	}

	/**
	 * @param y
	 */
	public void setY(int y) {
		this.y = y;
	}

	/**
	 * @return
	 */
	public int getYPadding() {
		return yPadding;
	}

	/**
	 * @param padding
	 */
	public void setYPadding(int padding) {
		yPadding = padding;
	}

	/**
	 * @return
	 */
	public int getRowCount() {
		return rowCount;
	}

	/**
	 * @return
	 */
	public int getNumberOfEmptyCellsAtEnd() {
		return numberOfEmptyCellsAtEnd;
	}

	/**
	 * @return
	 */
	public boolean isCompact() {
		return compact;
	}

	/**
	 * @param compact
	 */
	public void setCompact(boolean compact) {
		this.compact = compact;
	}

}
